﻿/*
 * API中的方法中，做三件事
 * 1、验证参数
 * 2、调用业务类
 * 3、统一返回值
 * **/
using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.DTO;
using Max.PH4_2102A.Shopping.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4_2102A.Shopping.API.Controllers
{
    /// <summary>
    /// 分类控制器
    /// </summary>
    [Route("category")]
    [ApiController]
    public class CategoryController : ControllerBase
    {

        ICategoryService _service;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="service"></param>
        public CategoryController(ICategoryService service)
        {
            _service = service;
        }

        /// <summary>
        /// 添加分类数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Insert(CategoryModel model)
        {
            ResponseResultDto result = new();

            //第一件：验证参数
            //if(string.IsNullOrEmpty(model.Sort))
            //{
            //    result.Code = ResponseStateCode.Failed;
            //    result.Message = "参数无效";
            //    return Ok(result);
            //}

            //第二件：调用业务
            int row = _service.Insert(model);
            result.Data = row;


            return Ok(result);
        }

        /// <summary>
        /// 递归查询分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SelectCategory()
        {
            ResponseResultDto result = new();

            var data = _service.SelectCategory();
            result.Data = data;

            return Ok(result);
        }
    }
}
